package com.gln.bloger.service.impl;

import java.util.List;

import org.hibernate.Hibernate;

import com.gln.bloger.constant.Constant;
import com.gln.bloger.dao.CommentDao;
import com.gln.bloger.dao.EntryDao;
import com.gln.bloger.exception.BlogerException;
import com.gln.bloger.log.Logging;
import com.gln.bloger.pojos.Comment;
import com.gln.bloger.pojos.Entry;
import com.gln.bloger.pojos.Website;
import com.gln.bloger.service.CommentService;
import com.gln.bloger.util.PagingSupport;

public class CommentServiceImpl implements CommentService {
	
	private CommentDao commentDao;
	
	private EntryDao entryDao;
	
	private List<Comment> cachedRecentComments;
	
	public EntryDao getEntryDao() {
		return entryDao;
	}
	
	
	public void setEntryDao(EntryDao entryDao) {
		this.entryDao = entryDao;
	}
	
	
	public CommentDao getCommentDao() {
		return commentDao;
	}
	
	
	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}
	
	public void remove(String id) throws BlogerException {
		Comment comment = this.getCommentDao().get(id);
		Hibernate.initialize(comment.getEntry());
		Entry entry = comment.getEntry();
		entry.setCommentSize(entry.getCommentSize()-1);
		getCommentDao().remove(comment);
	}
	
	public List<Comment> getComments(String entryId) throws BlogerException {
			Entry entry = getEntryDao().get(entryId);
			List<Comment> comments = getCommentDao().getByEntry(entry);
			return comments;
	}
	public PagingSupport<Comment> getAllComments(Website website)
		throws BlogerException {
	    PagingSupport<Comment> comment = new PagingSupport<Comment>();
	    List<Comment> commentDatas = getCommentDao().getAllComment(website);
	    comment.setData(commentDatas);
	    return comment;
	}
	
	public List<Comment> getRecentComments(Website website)throws BlogerException {
		if(this.cachedRecentComments !=null && this.cachedRecentComments.size() != 0){
			Logging.info("load recent comments from app cache");
		}else{
		    this.cachedRecentComments = getCommentDao().getRecentComments(website, Constant.RECENT_COMMENT_SIZE);
		}
		return this.cachedRecentComments;
	}
	
	
	public void saveOrUpdateComment(Comment comment, String entryId)
		throws BlogerException {
		    Entry entry = getEntryDao().get(entryId);
		    entry.setCommentSize(entry.getCommentSize()+1);
		    comment.setEntry(entry);
		    this.getCommentDao().saveOrUpdate(comment);
	}

	public void approve(String id) throws BlogerException {
		Comment comment = this.getCommentDao().get(id);
		Hibernate.initialize(comment);
		Hibernate.initialize(comment.getEntry());
		
		comment.setStatus(Comment.Status.APPROVED);
		this.getCommentDao().saveOrUpdate(comment);
		/**
		 * save entry manually to avoid hibernate assertion failure exception
		 * with out doing this step, hibernate will throw a assertion failure exception
		 * (entry.tags hasn't been flushed ) ,because entry has a collection property : tags 
		 */
		this.getEntryDao().saveOrUpdate(comment.getEntry());
	}

	public void clearCachedRecentComments() {
		if(this.cachedRecentComments != null){
			this.cachedRecentComments.clear();
		}
	}


	public List<Comment> getWaitForApproveComments(Website website)
			throws BlogerException {
		return getCommentDao().getWaitForApproveComments(website);
	}

}
